var openId;
var paras;
$(document).ready(function () {
    setWXConfig(preUrl + "Payment.html");

    document.getElementById('loadDiv').style.display = 'block';
    document.getElementById('payBtn').disabled = true;

    document.getElementById("bottom").style.marginTop = ((document.body.scrollHeight - document.body.clientHeight)-30) + "px";
    document.getElementById("bottom").style.display = "block";

    openId = getOpenIdByCookie();
    paras = getUrlParameters();
    var href = preUrl + "bill/queryBill/" + paras.id;
    $.ajax({
        type: "POST",
        url: href,
        async: true,
        dataType: "json",
        success: function (data) {
            if (typeof(data) == "undefined") {
                alert("访问服务器失败，请稍后重试");
                window.location.href = "PaymentQuery.html"
            } else {
                if (data.resCode == "0000") {
                    document.getElementById('loadDiv').style.display = 'none';
                    document.getElementById('allDiv').style.display = 'block';
                    $("#accountNo").text(data.accountNo);
                    $("#userName").text(data.userName);
                    $("#userAddr").text(data.userAddr);
                    $("#tradeDate").text(data.tradeDate);
                    $("#accountBalance").text(data.accountBalance);
                    $("#receiveFee").text(data.receiveFee);
                    var receiveFee = parseFloat(data.receiveFee);
                    if (receiveFee > 0) {
                        document.getElementById("ab").style.display = "none";
                        document.getElementById("rf").style.display = "block";
                    } else {
                        document.getElementById("rf").style.display = "none";
                        document.getElementById("ab").style.display = "block";
                    }
                    $("#tradeDate").text(data.tradeDate);
                    //$("#payFee").text(data.payFee);
                    if ("1" == data.isPay) {
                        document.getElementById('payBtn').disabled = false;
                        document.getElementById('payBtn').className = 'myButton';
                    }
                } else {
                    if (data.resCode == '9991') {
                        document.getElementById('loadDiv').style.display = 'none';
                        $('.cd-popup').addClass('is-visible');
                        //close popup
                        $('.cd-popup').on('click', function(event){
                            if( $(event.target).is('.cd-popup-close') || $(event.target).is('.cd-popup') ) {
                                event.preventDefault();
                                //$(this).removeClass('is-visible');
                                window.location.href = "PaymentQuery.html";
                            }
                        });
                    } else if (data.resCode == '9992') {
                        alert("" + data.errInfo);
                        window.location.href = "PaymentQuery.html";
                    } else {
                        alert("访问服务器失败，请稍后重试");
                        window.location.href = "PaymentQuery.html";
                    }
                }
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("访问服务器失败，请稍后重试");
            window.location.href = "PaymentQuery.html"
        }
    });
    loadWaiting();
});

function pay() {

    var payFee = $("#payFee").val();
    var receiveFee = $("#receiveFee").text();
    if (payFee == null || payFee == "") {
        alert("缴费金额不能为空");
        return;
    }
    if (isNaN(payFee)) {
        alert("缴费金额格式不对")
        return;
    }
    payFee = parseFloat(payFee);
    receiveFee = parseFloat(receiveFee);
    if (payFee < receiveFee) {
        alert("缴费金额不能少于欠费金额");
        return;
    }
    document.getElementById('payBtn').disabled = 'disabled';
    document.getElementById('payBtn').className = 'myButtonGray';
    document.getElementById('loadDiv').style.display = 'block';
    $.ajax({
        type: "POST",
        url: preUrl + "orderPay/" + getOpenIdByCookie() + "/" + paras.id,
        dataType: "json",
        data: {"payFee": payFee},
        async: true,
        success: function (data) {
            if (typeof(data) == "undefined") {
                document.getElementById('loadDiv').style.display = 'none';
                document.getElementById('payBtn').disabled = false;
                document.getElementById('payBtn').className = 'myButton';
                alert("访问服务器失败，请稍后重试");
            } else {
                if (data.resCode == "0000") {
                    if (data.token_id == "undefined" || data.token_id == "" || data.token_id == null) {
                        document.getElementById('loadDiv').style.display = 'none';
                        document.getElementById('payBtn').disabled = false;
                        document.getElementById('payBtn').className = 'myButton';
                        alert("获取支付签名失败，请稍后重试");
                    } else {
                        var token = data.token_id;
                        window.location.href = 'https://pay.swiftpass.cn/pay/jspay?token_id=' + token + '&showwxpaytitle=1';
                    }

                } else {
                    document.getElementById('loadDiv').style.display = 'none';
                    if (data.errInfo != "" && data.errInfo != null ) {
                        //$("#allDiv").html('');
                        document.getElementById('allDiv').style.display = 'block';
                        alert(data.errInfo);
                        if("1" == data.isPay) {
                            document.getElementById('payBtn').disabled = false;
                            document.getElementById('payBtn').className = 'myButton';
                        }
                    } else {
                        alert("访问服务器失败，请稍后重试");
                    }
                }
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            document.getElementById('loadDiv').style.display = 'none';
            document.getElementById('payBtn').disabled = false;
            document.getElementById('payBtn').className = 'myButton';
            alert("访问服务器失败，请稍后重试");
        }
    });
}